package com.kobe.classification.controller;

import cn.hutool.core.lang.tree.Tree;
import com.kobe.classification.domain.ClassificationDomain;
import com.kobe.classification.service.ClassificationService;
import com.kobe.common.core.domain.AjaxResult;
import com.kobe.common.core.page.TableDataInfo;
import com.kobe.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/kobe/classification")
public class ClassificationController {
    @Autowired
    private ClassificationService classificationService;

    //查询物料产品分类树
    @GetMapping("/treeV3")
    public List<Tree<String>> treeV3() {
        return classificationService.treeV3();
    }


    //根据ID查询物料产品分类
    @GetMapping(value = "/{categoryId}")
    public AjaxResult selectById(@PathVariable("categoryId") Long categoryId) {
        return AjaxResult.success(classificationService.selectById(categoryId));
    }

    //添加物料产品分类
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ClassificationDomain classificationDomain) {
        classificationDomain.setCreateBy(SecurityUtils.getUsername());
        classificationDomain.setCreateTime(LocalDateTime.now());
        classificationService.insert(classificationDomain);
        return AjaxResult.success("添加成功");
    }

    //更新物料产品分类
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ClassificationDomain classificationDomain) {
        classificationDomain.setUpdateBy(SecurityUtils.getUsername());
        classificationDomain.setUpdateTime(LocalDateTime.now());
        classificationService.update(classificationDomain);
        return AjaxResult.success("更新成功");
    }
}